home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / v10n22.arc / LTOA.ASM < prev    next >
Assembly Source File  |  1991-12-08  |  3KB  |  97 lines

  1.         title   LTOA --- Convert long (32-bit) integer to ASCII string
  2.         page    55,132
  3.  
  4. ; LTOA.ASM --- Convert long (32-bit) integer to ASCII string
  5. ;
  6. ; Copyright (C) 1989 Ray Duncan
  7. ;
  8. ; Call with:    DX:AX = 32-bit integer
  9. ;               DS:SI = buffer to receive string,
  10. ;                       must be at least 11 bytes long
  11. ;               CX    = radix
  12. ;
  13. ; Returns:      DS:SI = address of converted string
  14. ;               AX    = length of string
  15. ;
  16. ; Destroys:     BX, DX
  17. ; Since test for value = zero is made after a digit
  18. ; has been stored, the resulting string will always
  19. ; contain at least one significant digit.
  20.  
  21. _TEXT   segment word public 'CODE'
  22.  
  23.         assume  cs:_TEXT
  24.  
  25.         public  ltoa            
  26. ltoa    proc    near
  27.  
  28.         add     si,11                   ; advance to end of buffer
  29.         push    si                      ; and save that address.
  30.         or      dx,dx                   ; test sign of 32 bit value,
  31.         pushf                           ; and save sign on stack.
  32.         jns     ltoa1                   ; jump if value was positive.
  33.         not     dx                      ; it was negative, take 2's 
  34.         not     ax                      ; complement of the value. 
  35.         add     ax,1
  36.         adc     dx,0
  37.  
  38. ltoa1:  call    divide                  ; divide value by radix
  39.         add     bl,'0'                  ; convert remainder to ASCII digit
  40.         cmp     bl,'9'                  ; in case converting to hex ASCII,
  41.         jle     ltoa2                   ; jump if in range 0-9,
  42.         add     bl,'A'-'9'-1            ; correct digit if in range A-F.
  43.  
  44. ltoa2:  dec     si                      ; back up through buffer
  45.         mov     [si],bl                 ; store this character into string.
  46.         mov     bx,ax                   ; is value = zero yet?
  47.         or      bx,dx
  48.         jnz     ltoa1                   ; no, convert another digit.
  49.  
  50.         popf                            ; was original value negative?
  51.         jns     ltoa3                   ; no, jump
  52.         dec     si                      ; yes, store - sign into output
  53.         mov     byte ptr [si],'-'
  54.  
  55. ltoa3:  pop     ax                      ; calculate length of string
  56.         sub     ax,si
  57.         ret                             ; back to caller.
  58.  
  59. ltoa    endp
  60.  
  61. ;
  62. ; General purpose 32 bit by 16 bit unsigned divide.  This routine must 
  63. ; be used instead of the machine's usual unsigned divide for cases 
  64. ; where the quotient may overflow 16 bits (for example, 100,000 / 2 ).  
  65. ; If called with a zero divisor, this routine returns the dividend 
  66. ; unchanged and gives no warning.
  67. ;
  68. ; Call with:    DX:AX = 32 bit dividend
  69. ;               CX    = divisor
  70. ;
  71. ; Returns:      DX:AX = quotient
  72. ;               BX    = remainder
  73. ;               CX    = divisor (unchanged)
  74. ;
  75. ; Destroys:     Nothing
  76. ;
  77. divide  proc    near            
  78.  
  79.         jcxz    div1                    ; exit if divide by zero
  80.         push    ax                      ; 0:dividend_hi/divisor
  81.         mov     ax,dx
  82.         xor     dx,dx
  83.         div     cx
  84.         mov     bx,ax                   ; BX = quotient1
  85.         pop     ax                      ; remainder1:dividend_lo/divisor
  86.         div     cx
  87.         xchg    bx,dx                   ; DX:AX = quotient1:quotient2
  88.  
  89. div1:   ret                             ; BX = remainder2
  90.  
  91. divide  endp
  92.  
  93. _TEXT   ends
  94.  
  95.         end
  96.